/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetointegrado;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Emerson
 */
public class Server {
    ArrayList clientOutputStreams;
    ArrayList<PlayerMulti> players = new ArrayList<PlayerMulti>();
    public class ClientHandler implements Runnable{
      //  BufferedReader reader;
        public ObjectInputStream ois;
        PlayerMulti p;
        
        Socket sock;
        public ClientHandler(Socket clientSocket){
            try{
                
                sock = clientSocket;
                //ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
                ois = new ObjectInputStream(sock.getInputStream());
                //InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
                //reader = new BufferedReader(isReader);
                //players.add((PlayerMulti)ois.readObject());
                //System.out.println(players.size());
            }catch(Exception ex){ex.printStackTrace();}
        }
        @Override
        public void run() {
           // String message;
            
            
            try{
             //   ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
                while((p = (PlayerMulti) ois.readObject())!= null){
                    System.out.println("vai começar a receber o objeto");
                   // p = (PlayerMulti) ois.readObject();
                    System.out.println(p.nome);
                    
                    System.out.println("recebeu o objeto");
                   // System.out.println("read " + message);
                    tellEveryone(p);
                }
            }catch(Exception ex){ex.printStackTrace();}
            }
        }
    
//    public static void main(String[] args){
//        new Server().go();
//    }
        
    public void go(){
        clientOutputStreams = new ArrayList();
        try{
            ServerSocket serverSock = new ServerSocket(5900);
            while(true){
                System.out.println("aguardando conexao");
                Socket clientSocket = serverSock.accept();
               // PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
                ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
                clientOutputStreams.add(oos);
                
                Thread t = new Thread(new ClientHandler(clientSocket));
                t.start();
                System.out.println("nova conexao");
            }
        }catch(Exception ex){ex.printStackTrace();}
    }
    
    
    public void tellEveryone(PlayerMulti player){
        Iterator it = clientOutputStreams.iterator();
        while(it.hasNext()){
            try{ 
                ObjectOutputStream oos =(ObjectOutputStream) it.next();
                oos.writeObject(player);
                oos.flush();
               // PrintWriter writer = (PrintWriter) it.next();
               // writer.println(message);
               // writer.flush();
            }catch(Exception ex){ex.printStackTrace();}
        }
    }
    }

